<?php

class ApiMode {
	private $message = "";
	private $api = null ;
	private $auth = null;
    function ApiMode() {
		$this->auth = new Authority(true);
    }
    
    public function run() {
    		$p = '/@([\S]+)\s+(.*)/';
			preg_match($p, trim($_POST[Message]), $m);
			if(trim($m[2])=="") {
				$this->message = "参数有误0(>_<)0~" ;
				return ;
			}
			if($m[1]=="加群") {
				$this->enterCluster($m);
			}else if($m[1]=="退群") {
				$this->exitCluster($m);
			}else if($m[1]=="加好友") {
				$this->addFriedn($m);
			}else if($m[1]=="状态") {
				$this->changeStatus($m);
			}else if($m[1]=="stop") {
				$this->closeService($m);
			}else if($m[1]=="密钥") {
				$this->getKey($m);
			}else if($m[1]=="禁言") {
				$this->jinyan($m);
			}
			
    	
    }
    
    private function getKey($m) {
    	$this->api = new Api("GetRobotClientKey");
    	$this->message = $this->api->doApi(1,1);
    }
	 private function jinyan($m) {
		$lv = (int) $_POST[RobotAdmin];
		
    	$this->api = new Api("dissaymember");
		$pieces = explode(" ", trim($m[2]));
		$qq = "";
		$time = "";
		if(trim($pieces[0])!="1" &&  $this->auth->isClusterAdmin > 0) {
			$qq = $pieces[0];
		} else {
			$qq = $_POST[Sender];
			if($lv<1) {
				$this->message ="索妮娅成为管理员之日，就是禁言你之时~";
				return;
			}
		}
		$time = $pieces[1]?$pieces[1]:1;
		$this->api->Time = $time*60;
		//print_r($m);$this->message =
    	$this->api->doApi($_POST[ClusterNum],$qq);
    }
    private function enterCluster($m) {
    	$this->api = new Api("AddCluster");
    	$date_time = date("Y-m-d H:i:s", time() + 8 * 3600);
    	$enter_time = date("YmdHis", time() + 8 * 3600);
    	$arr = array (
				'ClusterNum' => trim($m[2]),
				'favorable' => 1,
				'update_time'=>$date_time,
				'enter_time' => $enter_time,
				'is_in_cluster'=>1,
				'is_service'=>1
			);
		@DB :: SaveTableRow('td_cluster_favorable', $arr);
    	$this->message = $this->api->doApi(trim($m[2]),"我是索妮娅酱0(>_<)0~~");
    	
    }
    
    private function exitCluster($m) {
    	$this->api = new Api("ExitCluster");
    	$this->message = $this->api->doApi(trim($m[2]),"退群");
    }
    
    private function addFriedn($m) {
    	$this->api = new Api("AddFriend");
    	$this->message = $this->api->doApi(trim($m[2]),"我是索妮娅酱0(>_<)0~~");
    }
    
    private function closeService($m) {
    	$this->api = new Api("Cluster");
    	$this->message = $this->api->doApi(trim($m[2]),null);
    	
    }
    private function changeStatus($m) {
    	$s = trim($m[2]);
    	if($s=="离开") {
    		$status = "away";
    	}else if($s=="在线") {
    		$status = "online";
    	}else if($s=="隐身") {
    		$status = "hidden";
    	}else if($s=="Q我吧") {
    		$status = "killme";
    	}else if($s=="离线") {
    		$status = "quiet";
    	}else{
    		$this->message = "参数出错!";
    		return;
    	}
    	$this->api = new Api("ChangeStatus");
    	$this->message = $this->api->doApi($status,"修改状态");
    }
    
	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}
}
?>